package cn.wy.org.controller;

import cn.wy.basic.util.AjaxResult;
import cn.wy.basic.util.PageList;
import cn.wy.org.domain.Employee;
import cn.wy.org.query.EmployeeQuery;
import cn.wy.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

//    GET http://localhost:80/dept/1    | 获取单个部门 |
    @GetMapping ("/{id}")
    public Employee findOne(@PathVariable ("id") Long id){
        Employee deptOne = employeeService.findOne(id);
        return deptOne;
    }
//    GET http://localhost:80/dept      | 获取所有部门 |
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }
//    Put http://localhost:80/dept     | 创建或者修改单个部门 |
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }

//    DELETE http://localhost:80/dept/1 | 删除单个资源 |
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
    //    Post http://localhost:80/dept      | 分页查询 |
    @PostMapping
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
       return employeeService.queryPage(employeeQuery);
    }
    // Patch http://localhost:8080/employee/1 批量删除
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
}
